import grails.test.*

class UserTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCreation() {
        City shanghai = new City(name:'shanghai')

        User existingUser = new User(name:'anybody',email:'anybody@anywhere',city:shanghai)
        mockDomain(User,[existingUser])

        User user = new User()
        assertFalse user.validate()
        assertEquals "nullable", user.errors["name"]
        assertEquals 'nullable', user.errors["email"]
        assertEquals 'nullable', user.errors["city"]
        assertEquals 'nullable', user.errors["gender"]
        assertEquals 'nullable', user.errors["friendGender"]

        user.name=''
        user.city = shanghai
        user.gender = ' '
        user.friendGender = ''
        user.email = ' '
        assertFalse user.validate()
        assertNull user.errors['city']
        assertEquals 'blank', user.errors['name']
        assertEquals 'blank', user.errors['gender']
        assertEquals 'blank', user.errors['friendGender']
        assertEquals 'blank', user.errors['email']

        user.name='anybody'
        user.email='Anybody@Anywhere'
        user.gender = 'N/A'
        user.friendGender = 'S'
        assertFalse user.validate()
        assertEquals 'unique', user.errors["name"]
        assertEquals 'validator', user.errors['email']
        assertEquals 'inList', user.errors['gender']
        assertEquals 'inList', user.errors['friendGender']
        
        user.name = 'AnyBody'
        user.gender = 'F'
        user.friendGender = 'N/A'
        user.email='a@b'
        assertTrue user.validate()

        user.email='A@B'
        assertTrue user.validate()

        mockDomain(BookInstance,[])
        
        user.ownedBooks = []
        user.ownedBooks << new BookInstance()
        //TODO should check that user cannot own and expect the same book
        assertTrue user.validate()
        user.save()
    }
}
